package com.ruixin.mvc.parser;

import com.ruixin.annotation.Parser;
import com.ruixin.base.BaseParser;
import com.ruixin.base.exception.BaseException;
import com.ruixin.base.util.BeanUtil;
import com.ruixin.mvc.annotation.Args;
import com.ruixin.mvc.config.ApplicationContextWrapper;
import com.ruixin.mvc.config.MethodMapping;
import com.ruixin.mvc.config.ParameterMapping;
import com.ruixin.mvc.config.WebMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@Parser(annotation= Args.class)
public class ArgsParser extends BaseParser<ApplicationContextWrapper> {

    public ArgsParser(ApplicationContextWrapper context, Class<? extends Annotation> type) {
        super(context, type);
    }

    @Override
    public void parseParamater(Object bean, Method method, Parameter parameter) throws BaseException {
        super.parseParamater(bean, method, parameter);
        WebMapping webMapping = this.context.getWebMapping(method.getDeclaringClass());
        Args args = BeanUtil.getAnnotation(parameter, Args.class);
        ParameterMapping argMapping = new ParameterMapping(parameter,args.value(),args.required(),true);
        MethodMapping methodMapping = webMapping.getMethodMapping(method);
        methodMapping.setArgMapping(parameter,argMapping);
    }
}
